package xm.components.text
{
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.FocusEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	import xm.core.XMSprite;
	
	
	/**
	 * <br> 클래스 설명 : TextInput 을 가지고있는 XMSprite 상속 객체</br>
	 * 
	 * 패딩은 bg 에 적용되지 않으며, textField, _defaultObject 에만 적용된다.
	 * <br />
	 * 기본 패딩값 : [11,11,11,11]
	 * <br />
	 * <br />
	 * @author 김정환
	 */	
	public class TextInput extends XMSprite
	{
		
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		
		/**
		 * 텍스트필드. 
		 */		
		protected var inputTF : TextField;
		
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		
		
		private var _bgClip : MovieClip;
		private var _defaultObject : Object;
		
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * TextInput<br />
		 * <br />
		 * <br> 클래스 설명 : TextInput 을 가지고있는 XMSprite 상속 객체</br>
		 * 
		 * 패딩은 bg 에 적용되지 않으며, textField, _defaultObject 에만 적용된다.
		 * <br />
		 * 기본 패딩값 : [11,11,11,11]
		 * <br />
		 * <br />
		 * @author 김정환
		 */	
		public function TextInput()
		{
			super();
		}
		

		/**
		 * commitProperties
		 */		
		override public function commitProperties():void
		{
			super.commitProperties();
			
		}
		
		
		
		/**
		 * updateDisplayList
		 */		
		override public function updateDisplayList( enforce : Boolean = false ):void
		{
			super.updateDisplayList(enforce);
			
			_bgClip.width = viewWidth;
			_bgClip.height = viewHeight;
			
			inputTF.x = paddingLeft;
			inputTF.y = paddingTop;
			
			inputTF.width = _bgClip.width - paddingLeft - paddingRight;
			inputTF.height = _bgClip.height - paddingTop - paddingBottom;
		}
		
		
		
		
		
		
		//-----------------------------------------------------------------------------------
		// life cycle functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * createChildren
		 */		
		override public function createChildren():void
		{
			super.createChildren();
			
			var tf : TextFormat;
			
			padding = [11,11,11,11];
			backgroundAlpha = 0;
			
			_bgClip = new XM_DEFAULT_SKIN_TEXTINPUT_BG();
			addChild( _bgClip );
			_bgClip.mouseChildren = false;
			_bgClip.mouseEnabled = false;
			
			tf = new TextFormat( "돋움", 20, 0x666666, true );
			
			inputTF = new TextField();
			inputTF.type = TextFieldType.INPUT;
			inputTF.defaultTextFormat = tf;
			inputTF.width = viewWidth;
			inputTF.height = _bgClip.height;
			
			addChild( inputTF );
			
			textField.addEventListener( FocusEvent.FOCUS_IN, onFocusIn );
			
		}
		
		
		/**
		 * createComplete
		 */		
		override public function creationComplete():void
		{
			super.creationComplete();
			
		}
		
		
		/**
		 * add
		 */		
		override public function add():void
		{
			super.add();
		}
		
		
		/**
		 * remove
		 */		
		override public function remove():void
		{
			super.remove();
		}
		
		
		/**
		 * destroy
		 */		
		override public function destroy():void
		{
			
			textField.removeEventListener( FocusEvent.FOCUS_IN, onFocusIn );
			textField.removeEventListener( FocusEvent.FOCUS_OUT, onFocusOut );
			
			
			if( inputTF )
			{
				if( inputTF.parent ) inputTF.parent.removeChild( inputTF );
				inputTF = null;
			}
			
			
			if( _bgClip )
			{
				if( _bgClip.parent ) _bgClip.parent.removeChild( _bgClip );
				_bgClip = null;
			}
			
			_defaultObject = null;
			
			
			super.destroy();
			
		}
		
		//-----------------------------------------------------------------------------------
		// setter / getter
		//-----------------------------------------------------------------------------------
		
		
		/**
		 * String 을 넣을경우 default text가 생성되며, 
		 * DisplayObject를 넣을경우 default 로 오브젝트가 붙어있음.
		 * @param value
		 * 
		 */		
		public function set defaultObject(value:Object):void
		{
			_defaultObject = value;
			
			if( _defaultObject is String )
			{
				inputTF.text = _defaultObject as String;
			}
			else
			{
				if( _defaultObject.hasOwnProperty("mouseEnabled") )
				{
					_defaultObject["mouseEnabled"] = false;
				}
				if( _defaultObject.hasOwnProperty("mouseChildren") )
				{
					_defaultObject["mouseChildren"] = false;
				}
				
				this.addChild( _defaultObject as DisplayObject );
			}
		}
		
		
		/**
		 * input 용 textField 반환 
		 * <br />
		 * @return : TextField - input 용 textField
		 */		
		public function get textField():TextField
		{
			return inputTF;
		}
		
		
		/**
		 * scale9grid가 적용된 무비클립 삽입.<br />
		 * <br />
		 * @param clip
		 */		
		public function set bgClip( clip : MovieClip ):void
		{
			if( _bgClip )
			{
				if( this.contains( _bgClip ) ) this.removeChild( _bgClip );
			}
			
			_bgClip = clip;
			this.addChildAt( _bgClip, 1 );
			
			_bgClip.mouseChildren = false;
			_bgClip.mouseEnabled = false;
		}
		
		//-----------------------------------------------------------------------------------
		// protected functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// internal functions
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		
		
		/**
		 * 기본버튼 활성화 및 비활성화 처리를 위한 이벤트 핸들러
		 * <br /> <br />
		 * @param e : FocusEvent
		 * 
		 */		
		protected function onFocusOut(e:FocusEvent):void
		{
			textField.removeEventListener(FocusEvent.FOCUS_OUT, onFocusOut );
			
			if( _defaultObject )
			{
				if( inputTF.text != ""  )
				{
					if( _defaultObject is String )
					{
						_defaultObject = null;
						textField.removeEventListener( FocusEvent.FOCUS_IN, onFocusIn );
					}
					else if( _defaultObject is DisplayObject )
					{
						_defaultObject = null;
						textField.removeEventListener( FocusEvent.FOCUS_IN, onFocusIn );
					}
				}
				else
				{
					defaultObject = _defaultObject;
				}
				
			}
			
			
		}		
		
		
		
		/**
		 * 기본버튼 활성화 및 비활성화 처리를 위한 이벤트 핸들러
		 * <br /><br />
		 * @param e
		 */		
		protected function onFocusIn(e:FocusEvent):void
		{
			textField.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut );
			
			if( _defaultObject )
			{
				if( _defaultObject is String )
				{
					inputTF.text = "";
				}
				else if( _defaultObject is DisplayObject )
				{
					removeChild( _defaultObject as DisplayObject );
				}
			}
		}
		
		
		
	}
}